package com.mego.megoandroid.activitys;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.mego.megoandroid.R;
import com.mego.megoandroid.database.dbmodel.SystemAllInfo;
import com.mego.megoandroid.database.manager.SystemCenter;
import com.mego.megoandroid.model.CallbackInfo;
import com.mego.megoandroid.utils.Constant;
import com.mego.megoandroid.version.UpdateManager;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import zuo.biao.library.base.BaseActivity;
import zuo.biao.library.ui.AlertDialog;
import zuo.biao.library.ui.ItemDialog;

public class IndoorMapActivity extends BaseActivity implements View.OnClickListener, ItemDialog.OnDialogItemClickListener, AlertDialog.OnDialogButtonClickListener {

    private final String TAG = "IndoorMapActivity";


    public static Intent createIntent(Context context) {
        return new Intent(context, IndoorMapActivity.class);
    }

    //启动方法>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.indoormap_activity);

        //apk版本升级
        UpdateManager.getUpdateManager().checkAppUpdate(this, false);

        initView();
        initData();
        initEvent();
    }

    @Override
    public void initView() {
        findViewById(R.id.tv_clear).setOnClickListener(this);
    }

    @Override
    public void initData() {

        initSysInfo();

    }


    @Override
    public void initEvent() {

    }

    @Override
    public void onForwardClick(View v) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.tv_clear:
                onClear();
                break;
        }
    }

    @Override
    public void onDialogButtonClick(int requestCode, boolean isPositive) {
        if (isPositive == false) {
            return;
        }
        if (requestCode == 100) {

        }
    }

    @Override
    public void onDialogItemClick(int requestCode, int position, String item) {

    }

    private void onClear() {

        if (mPB != null) {
            return;
        }

        mPB = ProgressDialog.show(this, "正在发送请求", "请求中...");

        SystemAllInfo info = SystemCenter.getInstance().getSysInfo("cashierid");
        if (info == null) {
            info = new SystemAllInfo(null, "cashierid", "2");
        }
        int cashierid = Integer.parseInt(info.getContent());

        //[{"goodsid_1":220,"price_1":0.01,"total_1":2},{"goodsid_2":219,"price_2":0.02,"total_2":1}]
        String goodsJson = "[{\"goodsid_1\":220,\"price_1\":0.01,\"total_1\":2},{\"goodsid_2\":219,\"price_2\":0.02,\"total_2\":1}]";

//        https://by.megowx.com/app/index.php?i=3&c=entry&m=ewei_shopv2&do=mobile&r=cashier.byapi.stockadd&cashierid=2
        String goods = "";
        try {
            goods = URLEncoder.encode(goodsJson, "utf-8");
        } catch (UnsupportedEncodingException e) {
            return;
        }

        JSONObject js_request = new JSONObject();//服务器需要传参的json对象
        try {
            js_request.put("goods", goods);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String url = Constant.URL_BASE + "&r=cashier.byapi.stockadd&cashierid=" + cashierid;
        RequestParams requestParams = new RequestParams(url);
        requestParams.setAsJsonContent(true);
        requestParams.setBodyContent(js_request.toString());
        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    CallbackInfo cc = CallbackInfo.parseJson(result);
                    System.out.println(cc.getResult().getMessage());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                System.out.println("errot");
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
            }
        });

    }

    ProgressDialog mPB;
    public static final int INITFINISH = 1101;

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == INITFINISH) {
                if (mPB != null) {
                    mPB.dismiss();
                    mPB = null;
                }
            }
        }

    };


    private final int REQUEST_TO_BOXVERSION = 25;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case REQUEST_TO_BOXVERSION:
                mHandler.sendEmptyMessage(INITFINISH);
                break;
        }
    }


    //初始化设备参数
    private void initSysInfo() {
        SystemAllInfo info = SystemCenter.getInstance().getSysInfo("cashierid");
        if (info == null) {
            info = new SystemAllInfo(null, "cashierid", "2");
        }
        SystemCenter.getInstance().saveSysInfo(info);
    }

}
